<template>
  <div class="message">
    <el-popover placement="bottom" :width="310" trigger="click">
      <template #reference>
        <el-badge :value="totalNum" class="item">
          <!-- <i :class="'iconfont icon-xiaoxi'" class="toolBar-icon"></i> -->
          <Bell style="width: 1em; height: 1em; margin-right: 8px" class="iconfont toolBar-icon" />
        </el-badge>
      </template>
      <el-tabs v-model="activeName">
        <el-tab-pane :label="'待办事项(' + todoNum + ')'" name="first">
          <div class="message-list">
            <div class="message-item" v-for="item in todoList" :key="item.label" @click="gotoMessageList">
              <img src="@/assets/images/msg02.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title" :title="item.title">{{ item.title }}</span>
                <span class="message-date">{{ item.sendtime }}</span>
              </div>
            </div>
          </div>
          <div class="message-empty" v-if="todoList == null || todoList.length < 1">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无消息</div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="'预警消息(' + warnNum + ')'" name="second">
          <div class="message-list">
            <div class="message-item" v-for="item in warnList" :key="item.label" @click="gotoMessageList">
              <img src="@/assets/images/msg01.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title" :title="item.title">{{ item.title }}</span>
                <span class="message-date">{{ item.sendtime }}</span>
              </div>
            </div>
          </div>
          <div class="message-empty" v-if="warnList == null || warnList.length < 1">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无消息</div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { getTodoMsgList, getWarnMsgList } from "@/api/modules/user";

const activeName = ref("first");
const router = useRouter();

const todoList = ref();
const todoNum = ref(0);

const warnList = ref();
const warnNum = ref(0);

// const totalNum = ref(0);

//待办消息
const getTodoList = async () => {
  const res: any = await getTodoMsgList({ pageSize: 5 });
  todoList.value = res.data.rows;
  todoNum.value = res.data.total;
};

//预警消息
const getWarnList = async () => {
  const res: any = await getWarnMsgList({ pageSize: 5 });
  warnList.value = res.data.rows;
  warnNum.value = res.data.total;
};

//跳转到消息中心
const gotoMessageList = () => {
  router.push({ name: "MessageList" });
};

const totalNum = computed(() => todoNum.value + warnNum.value);

getTodoList();
getWarnList();
</script>

<style scoped lang="scss">
.message-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 260px;
  line-height: 45px;
}
.message-list {
  display: flex;
  flex-direction: column;
  .message-item {
    display: flex;
    align-items: center;
    padding: 20px 0;
    border-bottom: 1px solid var(--el-border-color-light);
    &:last-child {
      border: none;
    }
    .message-icon {
      width: 40px;
      height: 40px;
      margin: 0 20px 0 5px;
    }
    .message-content {
      display: flex;
      flex-direction: column;
      .message-title {
        width: 200px; /* 超过该宽度将显示省略号 */
        margin-bottom: 5px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .message-date {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }
    }
  }
}
</style>
